//
//  ViewController.m
//  CollectionFlow
//
//  Created by bj_szd on 2025/5/6.
//

#import "ViewController.h"
#import "WalterFlowLayout.h"
#import "PictureCollectionViewCell.h"
// 渲染图片需要的头文件
#import <AVFoundation/AVFoundation.h>

@interface ViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, WalterFlowLayoutDelegate>

@property (nonatomic, strong) NSMutableArray *imageArr;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    NSArray *dataArr = @[
    [UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"2.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"3.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"4.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"5.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"6.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"7.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"2.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"3.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"4.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"5.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"6.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"7.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"2.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"3.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"4.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"5.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"6.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"7.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"2.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"3.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"4.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"5.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"6.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"7.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"2.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"7.jpg"]],
    [UIImage imageNamed:[NSString stringWithFormat:@"6.jpg"]]
    ];
    
    
//    NSArray *dataArr = @[
//    [UIImage imageNamed:[NSString stringWithFormat:@"1.jpg"]],
//    [UIImage imageNamed:[NSString stringWithFormat:@"2.jpg"]],
//    [UIImage imageNamed:[NSString stringWithFormat:@"3.jpg"]],
//    [UIImage imageNamed:[NSString stringWithFormat:@"4.jpg"]],
//    [UIImage imageNamed:[NSString stringWithFormat:@"5.jpg"]],
//    [UIImage imageNamed:[NSString stringWithFormat:@"6.jpg"]],
//    [UIImage imageNamed:[NSString stringWithFormat:@"7.jpg"]]
//    ];
    
    _imageArr = [NSMutableArray arrayWithArray:dataArr];
    [self.view addSubview:self.collectionView];
    
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _imageArr.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    PictureCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"PictureCollectionViewCell" forIndexPath:indexPath];
    UIImage *image = _imageArr[indexPath.item];
    cell.imageView.image = image;
    return cell;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout width:(CGFloat)width heightForItemAtIndexPath:(NSIndexPath *)indexPath {
    UIImage *image = self.imageArr[indexPath.item];
    // 根据传过来的宽度来设置一个合适的矩形,
    // 高度设为 CGFLOAT_MAX 表示以宽度来计算高度
    CGRect boundingRect = CGRectMake(0, 0, width, CGFLOAT_MAX);
    // 通过系统函数来得到最终的矩形。需要引入头文件 <AVFoundation/AVFoundation.h>
    CGRect imageCurrentRect = AVMakeRectWithAspectRatioInsideRect(image.size, boundingRect);
    return CGRectGetHeight(imageCurrentRect);
}

- (UICollectionView *)collectionView {
    if (!_collectionView) {
        WalterFlowLayout *layout = [[WalterFlowLayout alloc] init];
        layout.numberOfColumns = 4;
        layout.delegate = self;
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 50, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 50) collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [_collectionView registerClass:[PictureCollectionViewCell class] forCellWithReuseIdentifier:@"PictureCollectionViewCell"];
    }
    return _collectionView;
}


@end
